package me.magicall.myblog.front.web.springmvc.controllers;

import me.magicall.db.dao.Dao;
import me.magicall.db.model.BaseModel.NbBean;
import me.magicall.db.model.BaseModel.NbBean.NbBeanLong;
import me.magicall.db.util.DbUtil;
import me.magicall.myblog.util.DaoFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;

@Controller("oneController")
//@RequestMapping(method = RequestMethod.GET, //
//value = { UrlPatternRule.MODEL_NAME + "/" + UrlPatternRule.ID,//
//		UrlPatternRule.MODEL_NAME + "/one" + UrlPatternRule.ID, })
public class GetOneController {

	@Autowired
	private DaoFactory daoFactory;

//	@RequestMapping
	public void one(final ModelMap modelMap,//
			@PathVariable("modelName") final String modelName,//
			@PathVariable("id") final long id) {
		final Dao<NbBean<?>> dao = daoFactory.getNbBeanDao(modelName);
		final NbBean<?> bean = new NbBeanLong();
		if (id != 0) {
			bean.set(DbUtil.COMMON_ID_FIELD_NAME, id);
			modelMap.addAttribute("id", id);
			dao.get(bean);
		}

		modelMap.addAttribute("modelName", modelName);
		modelMap.addAttribute("cur", bean);
	}
}
